package com.ordersystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ordersystem.domain.User;
import com.ordersystem.mappers.UserMapper;
import com.ordersystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author : asus1020
 * @version : [v1.0]
 * @className : UserServiceImpl
 * @description : [userService层的实现类]
 * @createTime : [2022/3/20 17:02]
 * @updateUser : asus1020
 * @updateTime : [2022/3/20 17:02]
 * @updateRemark : [描述说明本次修改内容]
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override//登录方法
    public User login(User user) {
        User login = userMapper.login(user);
        return login;
    }

    @Override//注册方法
    public int regist(User user) {
        UUID uuid = UUID.randomUUID();
        user.setToken(String.valueOf(uuid).replace("-","").toUpperCase());
        return userMapper.regist(user);
    }

    @Override//检查用户名是否可用
    public int checkUsernameIsAvailable(String username) {
        return userMapper.checkUsernameIsAvailable(username);
    }

    @Override//查询用户列表数据
    public Map<String,Object> findAllUsers(Integer pagenum, Integer pagesize, String query) {
        HashMap<String, Object> map = new HashMap<>();
        Page<Object> page = PageHelper.startPage(pagenum, pagesize);
        List<User> userList = userMapper.findAllUsers(query);
        PageInfo<Object> pageInfo = new PageInfo<>(page.getResult());
        map.put("users",userList);
        map.put("totalpage",pageInfo.getTotal());
        map.put("pagenum",pageInfo.getPageNum());
        return map;
    }

    @Override//更新用户状态
    public int updateUserState(Integer uid, boolean state) {
        HashMap<String, Object> updateMap = new HashMap<>();
        updateMap.put("uid",uid);
        updateMap.put("state",state);
        int total = userMapper.updateUserState(updateMap);
        return total;
    }

    @Override//根据id查询用户
    public User findUserById(Integer uid) {
        return userMapper.findUserById(uid);
    }

    @Override//修改用户信息
    public User updateUser(User user) {
        int total = userMapper.updateUser(user);
        if (total>0){
            User userById = userMapper.findUserById(user.getUid());
            return userById;
        }

        return null;
    }

    @Override//删除用户
    public int deleteUserById(Integer uid) {
        return userMapper.deleteUserById(uid);
    }

    @Override//保存用户头像
    public int saveUserHeaderImg(String fileName, Integer uid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid",uid);
        map.put("fileName",fileName);
        int total = userMapper.saveUserHeaderImg(fileName,uid);
        return total;
    }


}
